package com.app.modules.sys.controller;

import com.app.common.annotation.SysLog;
import com.app.common.utils.PageUtils;
import com.app.common.utils.R;
import com.app.common.validator.AddGroup;
import com.app.common.validator.UpdateGroup;
import com.app.common.validator.ValidatorUtils;
import com.app.modules.sys.entity.SysUserEntity;
import com.app.modules.sys.service.SysUserRoleService;
import com.app.modules.sys.service.SysUserService;
import org.apache.commons.lang.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author : [ZhuHJ]
 * @version : [v1.0.0]
 * @description : [描述说明该类的功能]
 * @createTime : [2025/8/12 22:14]
 * @updateUser : [ZhuHJ]
 * @updateTime : [2025/8/12 22:14]
 * @updateRemark : [描述说明本次修改内容]
 */

@RestController
@RequestMapping("/sys/user")
public class SysUserController extends AbstractController{
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 所有用户列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){

        PageUtils page = sysUserService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 获取登录的用户信息
     */
    @GetMapping("/info")
    public R info(){
        return R.ok().put("user", getUser());
    }

    /**
     * 保存用户
     */
    @PostMapping("/save")
    @RequiresPermissions("sys:user:save")
    public R save(@RequestBody SysUserEntity user){
        ValidatorUtils.validateEntity(user, AddGroup.class);
        System.out.println("user:"+user.toString());
        user.setCreateUserId(getUserId());
        sysUserService.saveUser(user);

        return R.ok();
    }

    /**
     * 用户信息
     */
    @GetMapping("/info/{userId}")
    @RequiresPermissions("sys:user:info")
    public R info(@PathVariable("userId") Long userId){
        SysUserEntity user = sysUserService.getById(userId);

        //获取用户所属的角色列表
        List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);
        user.setRoleIdList(roleIdList);

        return R.ok().put("user", user);
    }

    /**
     * 修改用户
     */
    @PostMapping("/update")
    @RequiresPermissions("sys:user:update")
    @SysLog("修改用户信息")
    public R update(@RequestBody SysUserEntity user){
        ValidatorUtils.validateEntity(user, UpdateGroup.class);

        user.setCreateUserId(getUserId());
        sysUserService.update(user);

        return R.ok();
    }

    /**
     * 删除用户
     */
    @PostMapping("/delete")
    @RequiresPermissions("sys:user:delete")
    public R delete(@RequestBody Long[] userIds){
        if(ArrayUtils.contains(userIds, 1L)){
            return R.error("系统管理员不能删除");
        }

        if(ArrayUtils.contains(userIds, getUserId())){
            return R.error("当前用户不能删除");
        }
        System.out.println("userIds:"+userIds.toString());
        sysUserService.deleteBatch(userIds);

        return R.ok();
    }

}
